package com.beta.dto.order;

import com.beta.dto.product.ProductItemOptions;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.beta.dto.product.ProductPropertiesDto;
import com.beta.em.order.OrderItemStatus;
import com.beta.em.order.RefundStatus;
import com.beta.support.ProductPropertiesUtil;
import com.beta.support.ResourceImagePathBuilder;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by lichao on 2016/11/22.
 */
@Getter
@Setter
public class OrderItemDto implements Serializable {

    private static final long serialVersionUID = 8139218454223423417L;

    private Integer id;

    private Integer orderId;

    private String orderCode;

    private Integer productItemId;

    private String productItemName;

    private String productItemText;

    private String productItemSku;

    private String poster;

    private Integer quantity;

    private BigDecimal pricePerUnit;

    private Integer productId;

    // quantity * price_per_unit
//    private BigDecimal price;

    private String chargeUnit;

    private BigDecimal unitFreight;

    private String ownerName;

    private String status;

    private String orderStatus;

    private BigDecimal refundRate;

    private String refundStatus;

    private boolean rejectable;

    private BigDecimal unitWeight;

    private Integer userId;

    private Integer assetId;

    private String assetImage;

    @JsonIgnore
    private List<ProductPropertiesDto> properties;
    @JsonIgnore
    private List<ProductItemOptions> options;

    public String getPosterUrl() {
        if (StringUtils.isNoneBlank(getAssetImage())) {
            return ResourceImagePathBuilder.assetImagePath(getAssetId(), getAssetImage());
        }
        return ResourceImagePathBuilder.productPosterPath(getProductId(), this.getPoster());
    }

    public String getOrigin() {
        return ProductPropertiesUtil.getOrigin(properties);
    }

    public String getLevel() {
        return ProductPropertiesUtil.getLevel(options);
    }

    public String getName() {
        return String.join(",", productItemName, productItemText, getWeight() + "" + chargeUnit);
    }

    public String getStatusText() {
        return OrderItemStatus.getCnValue(this.getStatus());
    }

    public String getRefundStatusText() {
        return RefundStatus.getCnValue(this.getRefundStatus());
    }

    public BigDecimal getPrice() {
        return this.getPricePerUnit().multiply(new BigDecimal(this.getQuantity()));
    }

    public BigDecimal getWeight() {
        return unitWeight.multiply(new BigDecimal(quantity));
    }

    public BigDecimal getFreight() {
        return unitFreight.multiply(getWeight());
    }

}
